package com.woniuxy.service.impl;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.woniuxy.entity.Category;
import com.woniuxy.mapper.CategoryMapper;
import com.woniuxy.service.CategoryService;
import com.woniuxy.utils.KeyUtil;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dto.PageDto;
import com.woniuxy.entity.Category;
import com.woniuxy.mapper.CategoryMapper;
import com.woniuxy.service.CategoryService;
@Service
public class CategoryServiceImpl implements CategoryService{
	@Autowired
	private CategoryMapper categoryMapper;
	
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;

	@Override
	public PageDto<Category> findAllCategory(int page) {
		PageDto<Category> pageDto;
		synchronized (CategoryServiceImpl.class) { //加锁
			PageHelper.startPage(page, 10);
			List<Category> categorys = categoryMapper.findAllCategory();
			PageInfo<Category> pageInfo = new PageInfo<>(categorys);
			pageDto = new PageDto<Category>()
						.setCurrentPage(page)
						.setPageSize(pageInfo.getPageSize())
						.setTotal(pageInfo.getPageSize())
						.setTotalPages(pageInfo.getPages())
						.setData(categorys);
		}		
		return pageDto;	
	}

	@Override
	public int addCategory(Category category) {
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String resDate = dateFormat.format(date);
		category.setCgtime(resDate);
		return categoryMapper.addCategory(category);
	}

	@Override
	public PageDto<Category> findCategoryByStatus(int status, int page) {
		PageDto<Category> pageDto;
		synchronized (CategoryServiceImpl.class) {
			PageHelper.startPage(page, 10);
			List<Category> categorys = categoryMapper.findCategoryByStatus(status);
			PageInfo<Category> pageInfo = new PageInfo<>(categorys);
			pageDto = new PageDto<Category>()
						.setCurrentPage(page)
						.setPageSize(pageInfo.getPageSize())
						.setTotal(pageInfo.getPageSize())
						.setTotalPages(pageInfo.getPages())
						.setData(categorys);
		}
		return pageDto;
	}

	@Override
	public PageDto<Category> findCategoryByCgname(String cgname, int page) {
		PageDto<Category> pageDto;
		synchronized (CategoryServiceImpl.class) {
			PageHelper.startPage(page, 10);
			List<Category> categorys = categoryMapper.findCategoryByCgname(cgname);
			PageInfo<Category> pageInfo = new PageInfo<>(categorys);
			pageDto = new PageDto<Category>()
						.setCurrentPage(page)
						.setPageSize(pageInfo.getPageSize())
						.setTotal(pageInfo.getPageSize())
						.setTotalPages(pageInfo.getPages())
						.setData(categorys);
		}
		return pageDto;
	}

	@Override
	public int deleteCategoryByCgid(int cgid) {
		return categoryMapper.delectCategoryByCgid(cgid);
	}

	@Override
	public int updateSwitch(int cgid, int value) {
		int data;
		synchronized(CategoryServiceImpl.class) {
			Category category = new Category().setCgid(cgid).setStatus(value);
			data = categoryMapper.updateSwitch(category);
		}
		return data;
	}


	@Override
	public List<Category> findCategoryOfCgname() {
		String key=KeyUtil.generate(this, "findCategoryOfCgname");
		if (redisTemplate.hasKey(key)) {
			List<Object> objects=redisTemplate.opsForList().range(key, 0, redisTemplate.opsForList().size(key));
			List<Category> categories=new ArrayList<Category>();
			ArrayList<Object> data=(ArrayList<Object>) objects.get(0);
			for (Object object : data) {
				categories.add((Category)object);
			}
			return categories;
		}else{
			List<Category> categories=categoryMapper.findCategoryOfCgname();
			redisTemplate.opsForList().leftPushAll(key, categories);
			redisTemplate.expire(key, 1, TimeUnit.DAYS);
			return categories;
		}
	}

}
